#!/usr/bin/env node
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright (c) 2014, Joyent, Inc.
 */

amqp = require('./amqp');
sys  = require('sys');

var creds =
  { host:     process.env['AMQP_HOST']      || 'localhost'
  , port:     process.env['AMQP_PORT']      || 5672
  , login:    process.env['AMQP_LOGIN']     || 'guest'
  , password: process.env['AMQP_PASSWORD']  || 'guest'
  , vhost:    process.env['AMQP_VHOST']     || '/'
  };

var connection = amqp.createConnection(creds);
var exchange;

connection.addListener('ready', function () {
  exchange = connection.exchange('amq.topic', { type: 'topic' });
  var queue = connection.queue('ur.'+Math.random());
  queue.addListener("open", function () {
    console.log("Ready to receive messages");
  });

  queue.bind('amq.topic', 'ur.startup.#');
  queue.bind('amq.topic', 'ur.execute-reply.*.*');

  queue.subscribeJSON(function (m) {
    var rkParts = m._routingKey.split('.', 4);
    console.log("\"Head Node\" received message:");
    console.dir(m);

    if (rkParts[1] === "startup") {
      exchange.publish
        ( 'ur.execute.' + rkParts[2] + '.' + genId()
        , { type: 'file'
          , file: './my-test-script.sh'
          , args: []
          , env: {}
          }
        );
      exchange.publish
        ( 'ur.execute.' + rkParts[2] + '.' + genId()
        , { type:   'script'
          , script: "#!/bin/bash\necho hello world\nexit 0"
          , args: []
          , env: {}
          }
        );

      setTimeout(function () {
        exchange.publish
          ( 'ur.execute.' + rkParts[2] + '.' + genId()
          , { type:   'script'
            , script: "#!/bin/bash\necho goodbye, cruel world\nexit 113"
            , args: []
            , env: {}
            }
          );
      }, 2000);
    }
  });
});

function genId() {
  return Math.floor(Math.random() * 0xffffffff).toString(16);
};

// vim:ft=javascript
